home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / ntfs-3g / attrib.h next >
Encoding:
C/C++ Source or Header  |  2008-10-26  |  12.8 KB  |  347 lines

  1. /*
  2.  * attrib.h - Exports for attribute handling. Originated from the Linux-NTFS project.
  3.  *
  4.  * Copyright (c) 2000-2004 Anton Altaparmakov
  5.  * Copyright (c) 2004-2005 Yura Pakhuchiy
  6.  * Copyright (c) 2006-2007 Szabolcs Szakacsits
  7.  *
  8.  * This program/include file is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License as published
  10.  * by the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program/include file is distributed in the hope that it will be
  14.  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15.  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program (in the main directory of the NTFS-3G
  20.  * distribution in the file COPYING); if not, write to the Free Software
  21.  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  */
  23.  
  24. #ifndef _NTFS_ATTRIB_H
  25. #define _NTFS_ATTRIB_H
  26.  
  27. /* Forward declarations */
  28. typedef struct _ntfs_attr ntfs_attr;
  29. typedef struct _ntfs_attr_search_ctx ntfs_attr_search_ctx;
  30.  
  31. #include "types.h"
  32. #include "inode.h"
  33. #include "unistr.h"
  34. #include "runlist.h"
  35. #include "volume.h"
  36. #include "debug.h"
  37. #include "logging.h"
  38.  
  39. extern ntfschar AT_UNNAMED[];
  40.  
  41. /**
  42.  * enum ntfs_lcn_special_values - special return values for ntfs_*_vcn_to_lcn()
  43.  *
  44.  * Special return values for ntfs_rl_vcn_to_lcn() and ntfs_attr_vcn_to_lcn().
  45.  *
  46.  * TODO: Describe them.
  47.  */
  48. typedef enum {
  49.     LCN_HOLE        = -1,    /* Keep this as highest value or die! */
  50.     LCN_RL_NOT_MAPPED    = -2,
  51.     LCN_ENOENT        = -3,
  52.     LCN_EINVAL        = -4,
  53.     LCN_EIO            = -5,
  54. } ntfs_lcn_special_values;
  55.  
  56. /**
  57.  * struct ntfs_attr_search_ctx - search context used in attribute search functions
  58.  * @mrec:    buffer containing mft record to search
  59.  * @attr:    attribute record in @mrec where to begin/continue search
  60.  * @is_first:    if true lookup_attr() begins search with @attr, else after @attr
  61.  *
  62.  * Structure must be initialized to zero before the first call to one of the
  63.  * attribute search functions. Initialize @mrec to point to the mft record to
  64.  * search, and @attr to point to the first attribute within @mrec (not necessary
  65.  * if calling the _first() functions), and set @is_first to TRUE (not necessary
  66.  * if calling the _first() functions).
  67.  *
  68.  * If @is_first is TRUE, the search begins with @attr. If @is_first is FALSE,
  69.  * the search begins after @attr. This is so that, after the first call to one
  70.  * of the search attribute functions, we can call the function again, without
  71.  * any modification of the search context, to automagically get the next
  72.  * matching attribute.
  73.  */
  74. struct _ntfs_attr_search_ctx {
  75.     MFT_RECORD *mrec;
  76.     ATTR_RECORD *attr;
  77.     BOOL is_first;
  78.     ntfs_inode *ntfs_ino;
  79.     ATTR_LIST_ENTRY *al_entry;
  80.     ntfs_inode *base_ntfs_ino;
  81.     MFT_RECORD *base_mrec;
  82.     ATTR_RECORD *base_attr;
  83. };
  84.  
  85. extern void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx);
  86. extern ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni,
  87.         MFT_RECORD *mrec);
  88. extern void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx);
  89.  
  90. extern int ntfs_attr_lookup(const ATTR_TYPES type, const ntfschar *name,
  91.         const u32 name_len, const IGNORE_CASE_BOOL ic,
  92.         const VCN lowest_vcn, const u8 *val, const u32 val_len,
  93.         ntfs_attr_search_ctx *ctx);
  94.  
  95. extern ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
  96.         const ATTR_TYPES type);
  97.  
  98. /**
  99.  * ntfs_attrs_walk - syntactic sugar for walking all attributes in an inode
  100.  * @ctx:    initialised attribute search context
  101.  *
  102.  * Syntactic sugar for walking attributes in an inode.
  103.  *
  104.  * Return 0 on success and -1 on error with errno set to the error code from
  105.  * ntfs_attr_lookup().
  106.  *
  107.  * Example: When you want to enumerate all attributes in an open ntfs inode
  108.  *        @ni, you can simply do:
  109.  *
  110.  *    int err;
  111.  *    ntfs_attr_search_ctx *ctx = ntfs_attr_get_search_ctx(ni, NULL);
  112.  *    if (!ctx)
  113.  *        // Error code is in errno. Handle this case.
  114.  *    while (!(err = ntfs_attrs_walk(ctx))) {
  115.  *        ATTR_RECORD *attr = ctx->attr;
  116.  *        // attr now contains the next attribute. Do whatever you want
  117.  *        // with it and then just continue with the while loop.
  118.  *    }
  119.  *    if (err && errno != ENOENT)
  120.  *        // Ooops. An error occurred! You should handle this case.
  121.  *    // Now finished with all attributes in the inode.
  122.  */
  123. static __inline__ int ntfs_attrs_walk(ntfs_attr_search_ctx *ctx)
  124. {
  125.     return ntfs_attr_lookup(AT_UNUSED, NULL, 0, CASE_SENSITIVE, 0,
  126.             NULL, 0, ctx);
  127. }
  128.  
  129. /**
  130.  * struct ntfs_attr - ntfs in memory non-resident attribute structure
  131.  * @rl:            if not NULL, the decompressed runlist
  132.  * @ni:            base ntfs inode to which this attribute belongs
  133.  * @type:        attribute type
  134.  * @name:        Unicode name of the attribute
  135.  * @name_len:        length of @name in Unicode characters
  136.  * @state:        NTFS attribute specific flags describing this attribute
  137.  * @allocated_size:    copy from the attribute record
  138.  * @data_size:        copy from the attribute record
  139.  * @initialized_size:    copy from the attribute record
  140.  * @compressed_size:    copy from the attribute record
  141.  * @compression_block_size:        size of a compression block (cb)
  142.  * @compression_block_size_bits:    log2 of the size of a cb
  143.  * @compression_block_clusters:        number of clusters per cb
  144.  *
  145.  * This structure exists purely to provide a mechanism of caching the runlist
  146.  * of an attribute. If you want to operate on a particular attribute extent,
  147.  * you should not be using this structure at all. If you want to work with a
  148.  * resident attribute, you should not be using this structure at all. As a
  149.  * fail-safe check make sure to test NAttrNonResident() and if it is false, you
  150.  * know you shouldn't be using this structure.
  151.  *
  152.  * If you want to work on a resident attribute or on a specific attribute
  153.  * extent, you should use ntfs_lookup_attr() to retrieve the attribute (extent)
  154.  * record, edit that, and then write back the mft record (or set the
  155.  * corresponding ntfs inode dirty for delayed write back).
  156.  *
  157.  * @rl is the decompressed runlist of the attribute described by this
  158.  * structure. Obviously this only makes sense if the attribute is not resident,
  159.  * i.e. NAttrNonResident() is true. If the runlist hasn't been decompressed yet
  160.  * @rl is NULL, so be prepared to cope with @rl == NULL.
  161.  *
  162.  * @ni is the base ntfs inode of the attribute described by this structure.
  163.  *
  164.  * @type is the attribute type (see layout.h for the definition of ATTR_TYPES),
  165.  * @name and @name_len are the little endian Unicode name and the name length
  166.  * in Unicode characters of the attribute, respectively.
  167.  *
  168.  * @state contains NTFS attribute specific flags describing this attribute
  169.  * structure. See ntfs_attr_state_bits above.
  170.  */
  171. struct _ntfs_attr {
  172.     runlist_element *rl;
  173.     ntfs_inode *ni;
  174.     ATTR_TYPES type;
  175.     ntfschar *name;
  176.     u32 name_len;
  177.     unsigned long state;
  178.     s64 allocated_size;
  179.     s64 data_size;
  180.     s64 initialized_size;
  181.     s64 compressed_size;
  182.     u32 compression_block_size;
  183.     u8 compression_block_size_bits;
  184.     u8 compression_block_clusters;
  185. };
  186.  
  187. /**
  188.  * enum ntfs_attr_state_bits - bits for the state field in the ntfs_attr
  189.  * structure
  190.  */
  191. typedef enum {
  192.     NA_Initialized,        /* 1: structure is initialized. */
  193.     NA_NonResident,        /* 1: Attribute is not resident. */
  194. } ntfs_attr_state_bits;
  195.  
  196. #define  test_nattr_flag(na, flag)     test_bit(NA_##flag, (na)->state)
  197. #define   set_nattr_flag(na, flag)      set_bit(NA_##flag, (na)->state)
  198. #define clear_nattr_flag(na, flag)    clear_bit(NA_##flag, (na)->state)
  199.  
  200. #define NAttrInitialized(na)         test_nattr_flag(na, Initialized)
  201. #define NAttrSetInitialized(na)          set_nattr_flag(na, Initialized)
  202. #define NAttrClearInitialized(na)    clear_nattr_flag(na, Initialized)
  203.  
  204. #define NAttrNonResident(na)         test_nattr_flag(na, NonResident)
  205. #define NAttrSetNonResident(na)          set_nattr_flag(na, NonResident)
  206. #define NAttrClearNonResident(na)    clear_nattr_flag(na, NonResident)
  207.  
  208. #define GenNAttrIno(func_name, flag)            \
  209. extern int NAttr##func_name(ntfs_attr *na);        \
  210. extern void NAttrSet##func_name(ntfs_attr *na);        \
  211. extern void NAttrClear##func_name(ntfs_attr *na);
  212.  
  213. GenNAttrIno(Compressed, FILE_ATTR_COMPRESSED)
  214. GenNAttrIno(Encrypted,     FILE_ATTR_ENCRYPTED)
  215. GenNAttrIno(Sparse,     FILE_ATTR_SPARSE_FILE)
  216. #undef GenNAttrIno
  217.  
  218. /**
  219.  * union attr_val - Union of all known attribute values
  220.  *
  221.  * For convenience. Used in the attr structure.
  222.  */
  223. typedef union {
  224.     u8 _default;    /* Unnamed u8 to serve as default when just using
  225.                a_val without specifying any of the below. */
  226.     STANDARD_INFORMATION std_inf;
  227.     ATTR_LIST_ENTRY al_entry;
  228.     FILE_NAME_ATTR filename;
  229.     OBJECT_ID_ATTR obj_id;
  230.     SECURITY_DESCRIPTOR_ATTR sec_desc;
  231.     VOLUME_NAME vol_name;
  232.     VOLUME_INFORMATION vol_inf;
  233.     DATA_ATTR data;
  234.     INDEX_ROOT index_root;
  235.     INDEX_BLOCK index_blk;
  236.     BITMAP_ATTR bmp;
  237.     REPARSE_POINT reparse;
  238.     EA_INFORMATION ea_inf;
  239.     EA_ATTR ea;
  240.     PROPERTY_SET property_set;
  241.     LOGGED_UTILITY_STREAM logged_util_stream;
  242.     EFS_ATTR_HEADER efs;
  243. } attr_val;
  244.  
  245. extern void ntfs_attr_init(ntfs_attr *na, const BOOL non_resident,
  246.         const BOOL compressed, const BOOL encrypted, const BOOL sparse,
  247.         const s64 allocated_size, const s64 data_size,
  248.         const s64 initialized_size, const s64 compressed_size,
  249.         const u8 compression_unit);
  250.  
  251. extern ntfs_attr *ntfs_attr_open(ntfs_inode *ni, const ATTR_TYPES type,
  252.         ntfschar *name, u32 name_len);
  253. extern void ntfs_attr_close(ntfs_attr *na);
  254.  
  255. extern s64 ntfs_attr_pread(ntfs_attr *na, const s64 pos, s64 count,
  256.         void *b);
  257. extern s64 ntfs_attr_pwrite(ntfs_attr *na, const s64 pos, s64 count,
  258.         const void *b);
  259.  
  260. extern void *ntfs_attr_readall(ntfs_inode *ni, const ATTR_TYPES type,
  261.                    ntfschar *name, u32 name_len, s64 *data_size);
  262.  
  263. extern s64 ntfs_attr_mst_pread(ntfs_attr *na, const s64 pos,
  264.         const s64 bk_cnt, const u32 bk_size, void *dst);
  265. extern s64 ntfs_attr_mst_pwrite(ntfs_attr *na, const s64 pos,
  266.         s64 bk_cnt, const u32 bk_size, void *src);
  267.  
  268. extern int ntfs_attr_map_runlist(ntfs_attr *na, VCN vcn);
  269. extern int ntfs_attr_map_whole_runlist(ntfs_attr *na);
  270.  
  271. extern LCN ntfs_attr_vcn_to_lcn(ntfs_attr *na, const VCN vcn);
  272. extern runlist_element *ntfs_attr_find_vcn(ntfs_attr *na, const VCN vcn);
  273.  
  274. extern int ntfs_attr_size_bounds_check(const ntfs_volume *vol,
  275.         const ATTR_TYPES type, const s64 size);
  276. extern int ntfs_attr_can_be_non_resident(const ntfs_volume *vol,
  277.         const ATTR_TYPES type);
  278. extern int ntfs_attr_can_be_resident(const ntfs_volume *vol,
  279.         const ATTR_TYPES type);
  280.  
  281. extern int ntfs_make_room_for_attr(MFT_RECORD *m, u8 *pos, u32 size);
  282.  
  283. extern int ntfs_resident_attr_record_add(ntfs_inode *ni, ATTR_TYPES type,
  284.         ntfschar *name, u8 name_len, u8 *val, u32 size,
  285.         ATTR_FLAGS flags);
  286. extern int ntfs_non_resident_attr_record_add(ntfs_inode *ni, ATTR_TYPES type,
  287.         ntfschar *name, u8 name_len, VCN lowest_vcn, int dataruns_size,
  288.         ATTR_FLAGS flags);
  289. extern int ntfs_attr_record_rm(ntfs_attr_search_ctx *ctx);
  290.  
  291. extern int ntfs_attr_add(ntfs_inode *ni, ATTR_TYPES type,
  292.         ntfschar *name, u8 name_len, u8 *val, s64 size);
  293. extern int ntfs_attr_rm(ntfs_attr *na);
  294.  
  295. extern int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size);
  296.  
  297. extern int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
  298.         const u32 new_size);
  299.  
  300. extern int ntfs_attr_record_move_to(ntfs_attr_search_ctx *ctx, ntfs_inode *ni);
  301. extern int ntfs_attr_record_move_away(ntfs_attr_search_ctx *ctx, int extra);
  302.  
  303. extern int ntfs_attr_update_mapping_pairs(ntfs_attr *na, VCN from_vcn);
  304.  
  305. extern int ntfs_attr_truncate(ntfs_attr *na, const s64 newsize);
  306.  
  307. /**
  308.  * get_attribute_value_length - return the length of the value of an attribute
  309.  * @a:    pointer to a buffer containing the attribute record
  310.  *
  311.  * Return the byte size of the attribute value of the attribute @a (as it
  312.  * would be after eventual decompression and filling in of holes if sparse).
  313.  * If we return 0, check errno. If errno is 0 the actual length was 0,
  314.  * otherwise errno describes the error.
  315.  *
  316.  * FIXME: Describe possible errnos.
  317.  */
  318. extern s64 ntfs_get_attribute_value_length(const ATTR_RECORD *a);
  319.  
  320. /**
  321.  * get_attribute_value - return the attribute value of an attribute
  322.  * @vol:    volume on which the attribute is present
  323.  * @a:        attribute to get the value of
  324.  * @b:        destination buffer for the attribute value
  325.  *
  326.  * Make a copy of the attribute value of the attribute @a into the destination
  327.  * buffer @b. Note, that the size of @b has to be at least equal to the value
  328.  * returned by get_attribute_value_length(@a).
  329.  *
  330.  * Return number of bytes copied. If this is zero check errno. If errno is 0
  331.  * then nothing was read due to a zero-length attribute value, otherwise
  332.  * errno describes the error.
  333.  */
  334. extern s64 ntfs_get_attribute_value(const ntfs_volume *vol, 
  335.                     const ATTR_RECORD *a, u8 *b);
  336.  
  337. extern void  ntfs_attr_name_free(char **name);
  338. extern char *ntfs_attr_name_get(const ntfschar *uname, const int uname_len);
  339. extern int   ntfs_attr_exist(ntfs_inode *ni, const ATTR_TYPES type,
  340.                  ntfschar *name, u32 name_len);
  341. extern int   ntfs_attr_remove(ntfs_inode *ni, const ATTR_TYPES type,
  342.                   ntfschar *name, u32 name_len);
  343. extern s64   ntfs_attr_get_free_bits(ntfs_attr *na);
  344.  
  345. #endif /* defined _NTFS_ATTRIB_H */
  346.  
  347.